#include "proxyimpl.h"
#include "QApplication.h"
#include "zonesmanager.h"
#include <optra/zoneinterface.h>
#include <QHostInfo>
#include "optrzone.h"
#include "application.h"
#include <optra/common/gui.h>

ProxyImpl::ProxyImpl(Application* main)
    :Proxy(NULL), main_(main){
    gui_ = new Optra::Gui(main);

}

ProxyImpl::~ProxyImpl()
{
    delete gui_;
}

Optra::Gui* ProxyImpl::gui()
{
    return gui_;
}

QDir ProxyImpl::directory(Optra::Proxy::DirType d)
{
    QDir dir;
    switch(d){
    case Proxy::program:
        dir = QDir(qApp->applicationDirPath());
        break;
    case Proxy::user:
        dir = QDir::home();
        break;
    case Proxy::temp:
        dir =  QDir::temp();
        break;
    case Proxy::zones:
        dir =  this->directory(Proxy::program);
        dir.cd("zones");
        break;
    case Proxy::applications:
        dir =  this->directory(Proxy::program);
        dir.cd("applications");
        break;
    default:
        dir = QDir::current();
    }

    return dir;
}

Optra::ZoneInterface* ProxyImpl::mainZone()
{
    return main_->zones()->optra();
}

Optra::ZoneInterface* ProxyImpl::zoneInterface(const QString& name)
{
    return main_->zones()->zoneInterface(name);
}

QVariant ProxyImpl::zoneProperty(const QString& zone, const QString& name, const QVariant& params)
{
    Optra::ZoneInterface* zi = zoneInterface(zone);
    if (zi == NULL){
        return QVariant();
    }
    zi->setProperty(name.toAscii().data(), params);
    QVariant v = zi->property(name.toAscii().data());
    return v;
}

QString ProxyImpl::localHostName()
{
    QString hostname = QHostInfo::localHostName();
    int i = hostname.indexOf('.');
    if (i != -1)
        return hostname.left(hostname.indexOf('.'));
    else
        return hostname;
}
